package com.das.mechanic_base.utils;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.das.mechanic_base.R;

import java.util.List;

public class X3BaseDialogUtil1 extends Dialog {

    private Activity context;
    private int itemLayoutId;
    private List<View> list;

    private Handler mHandler = new Handler();

    Runnable runnable = () -> dismiss();

    public void showMinuteDismiss() {
        mHandler.postDelayed(runnable,2500);
    }

    @Override
    public void dismiss() {
        if (mHandler != null) {
            mHandler.removeCallbacks(runnable);
        }
        super.dismiss();
    }

    private Point mScreenPoint = new Point();

    /**
     * 无高度设置、居中显示
     *
     * @param mContext
     * @param view
     * @return
     */
    public static X3BaseDialogUtil1 showDialog(Activity mContext, int view) {
        X3BaseDialogUtil1 dialog = new X3BaseDialogUtil1(mContext, R.style.PhotoDialog, view);
        dialog.show();
        return dialog;
    }

    public static X3BaseDialogUtil1 showDialog(Activity mContext, int view, String str) {
        X3BaseDialogUtil1 dialog = new X3BaseDialogUtil1(mContext, R.style.PhotoDialog, view);
        dialog.show();
        return dialog;
    }

    public static X3BaseDialogUtil1 showDialog(Activity mContext, int view, boolean flag) {
        X3BaseDialogUtil1 dialog = new X3BaseDialogUtil1(mContext, view);
        dialog.show();
        return dialog;
    }

    /**
     * 距离头部、底部的高度
     *
     * @param mContext
     * @param view
     * @param y
     * @return
     */
    public static X3BaseDialogUtil1 showDialog(Activity mContext, int view, int mGravity, int y) {
        X3BaseDialogUtil1 dialog = new X3BaseDialogUtil1(mContext, R.style.PhotoDialog, view);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(mGravity);
        lp.y = y; // 新位置Y坐标
        dialogWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }

    public X3BaseDialogUtil1(Activity context, int theme, int itemLayoutId) {
        super(context, theme);
        this.context = context;
        this.itemLayoutId = itemLayoutId;

    }

    public X3BaseDialogUtil1(Activity context, int itemLayoutId) {
        super(context);
        this.context = context;
        this.itemLayoutId = itemLayoutId;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(itemLayoutId);
        this.setCanceledOnTouchOutside(false);
        initDialogWindow();
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = null;
        view = findViewById(viewId);
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public X3BaseDialogUtil1 setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    @SuppressWarnings("deprecation")
    private void initDialogWindow() {
        Window dialogWindow = getWindow();

        try {
            dialogWindow.setGravity(Gravity.CENTER);
        } catch (Exception e) {
            e.printStackTrace();
        }

        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mScreenPoint.x * 0.7);    // 宽度设置为屏幕的0.8
        // p.y = - (int) (mScreenPoint.y * 0.1);
        //  p.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(p);
    }

    @Override
    public void setOnDismissListener(@Nullable OnDismissListener listener) {
        super.setOnDismissListener(listener);
    }
}
